package com.sh.data.engine.web.webapi.controller.api;

import com.sh.data.engine.application.model.dto.api.*;
import com.sh.data.engine.application.model.dto.api.DataApiResponse.DataApiParamResponse;
import com.sh.data.engine.application.model.dto.common.DirectoryResponse;
import com.sh.data.engine.application.model.dto.integration.OfflinePreviewApiDataResponse;
import com.sh.data.engine.application.model.dto.normalization.index.IndexCatalogResponse;
import com.sh.data.engine.application.model.request.api.*;
import com.sh.data.engine.application.service.api.DataApiApplicationService;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.domain.shims.db.model.TableInfoDomain;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : fuchen
 * @date : 2022/1/19 15:29
 */
@Validated
@RestController
@RequestMapping("/dataApi")
@Tag(name = "数据服务-API管理-我的API", description = "数据服务-API管理-我的API")
@Slf4j
public class DataApiController {

    @Autowired
    private DataApiApplicationService dataApiApplicationService;

    @PostMapping("/saveGuideApi")
    @Operation(summary = "保存向导模式API")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.API)
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.数据服务)
//    @Log(business = Business.数据服务, operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    RestResponseEntity<Boolean> saveGuideApi(@RequestBody DataApiGuideRequest dataApiGuideRequest) {
        var result = dataApiApplicationService.saveGuideApi(dataApiGuideRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/listCatalog")
    @Operation(summary = "获取指标目录(后端没有做树形结构封装)")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<List<IndexCatalogResponse>> listCatalog() {
        var result = dataApiApplicationService.listCatalog();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getPublishedIndex")
    @Operation(summary = "获取指标列表，如果某个指标有多个版本，那么返回最新的版本")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    RestResponseEntity<List<DataApiIndexResponse>> getDataApiIndexList(Long catalogId) {
        var result = dataApiApplicationService.getDataApiIndexList(catalogId);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/saveIndexApi")
    @Operation(summary = "保存指标API")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.API)
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.数据服务)
//    @Log(business = Business.数据服务, operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    RestResponseEntity<Boolean> saveIndexApi(@RequestBody DataApiIndexRequest dataApiIndexRequest) {
        var result = dataApiApplicationService.saveIndexApi(dataApiIndexRequest);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/saveScriptApi")
    @Operation(summary = "保存脚本模式API")
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.数据服务)
//    @Log(business = Business.数据服务, operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.API)
    RestResponseEntity<Boolean> saveScriptApi(
        @RequestBody DataApiScriptRequest dataApiScriptRequest) {
        var result = dataApiApplicationService.saveScriptApi(dataApiScriptRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getParams")
    @Operation(summary = "根据脚本获取参数")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<List<DataApiParamResponse>> getParams(
        @Parameter(description = "脚本", required = true) @RequestParam String sql,
        @Parameter(description = "数据源id") @RequestParam(name = "dsId", required = false) Long dsId) {
        var result = dataApiApplicationService.getParams(sql, dsId);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/saveRegisterApi")
    @Operation(summary = "保存注册模式API")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.API)
    @LogExecution(operation = EngineOperation.ADD_UPDATE, business = Business.数据服务)
//    @Log(business = Business.数据服务, operation = Operation.ADD_UPDATE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    RestResponseEntity<Boolean> saveRegisterApi(
        @RequestBody DataApiRegisterRequest dataApiRegisterRequest) {
        var result = dataApiApplicationService.saveRegisterApi(dataApiRegisterRequest);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "获取api组")
    @GetMapping("/getApiGroup")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<List<DirectoryResponse>> getDataApiGroupList() {
        var result = dataApiApplicationService.getDataApiGroupList();
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getDataSourceList")
    @Operation(summary = "获取数据源列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    RestResponseEntity<List<DataSourceSimpleResponse>> getDataSourceList(
        @Parameter(description = "开发模式") @RequestParam(name = "apiType", required = false) Integer apiType) {
        var result = dataApiApplicationService.getDataSourceList(apiType);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getTableInfoList")
    @Operation(summary = "获取表列表供选择")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    RestResponseEntity<List<TableInfoDomain>> getTableInfoList(@RequestParam(name = "dsId") Long id) {
        var result = dataApiApplicationService.getTableInfoList(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/previewData")
    @Operation(summary = "预览数据")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<OfflinePreviewApiDataResponse> previewData(
        @Parameter(description = "数据源id") @RequestParam(value = "dsId") Long dsId,
        @Parameter(description = "表名") @RequestParam(value = "tableName") String tableName) {
        var result = dataApiApplicationService.previewData(dsId, tableName);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getTableFieldList")
    @Operation(summary = "获取字段列表")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    RestResponseEntity<List<DataApiSaveFieldResponse>> getTableFieldList(
        @RequestParam(name = "dsId") Long dsId, @RequestParam(name = "tableName") String tableName) {
        var result = dataApiApplicationService.getTableFieldList(dsId, tableName);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getDataApiDetailById")
    @Operation(summary = "获取api详情")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    RestResponseEntity<DataApiResponse> getDataApiDetailById(Long id) {
        var result = dataApiApplicationService.getDataApiDetailById(id);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getResponseExample")
    @Operation(summary = "返回api测试用例")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<String> getResponseExample(
        @RequestBody DataApiExampleTestRequest dataApiExampleTestRequest) {
        var result = dataApiApplicationService.getResponseExample(dataApiExampleTestRequest);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/tesParamApi")
    @Operation(summary = "测试Api")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Long> testGuideApi(
        @RequestBody DataApiTestRequest dataApiGuideRequest) {
        var result = dataApiApplicationService.testGuidApi(dataApiGuideRequest);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/testRegisterApi")
    @Operation(summary = "测试Api")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 1)
    public RestResponseEntity<Long> testRegisterApi(
        @RequestBody DataApiRegisterRequest dataApiRegisterRequest) {
        var result = dataApiApplicationService.testRegisterApi(dataApiRegisterRequest);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/log")
    @Operation(summary = "查看日志")
//    @AuthorityAnnotation(menuId = MenuEnumNew.WDAPI, readWriteStatus = 0)
    public RestResponseEntity<DataApiLogResponse> getRequestLog(
        @Parameter(description = "请求id", required = true) @RequestParam("logId") Long logId,
        @Parameter(description = "偏移量", required = true) @RequestParam("offset") Integer offset) {
        var result = dataApiApplicationService.getRequestLog(logId, offset);
        return RestResponseEntity.success(result);
    }
}
